<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}" media="all">

    <script th:src="@{/static/layui/layui.js}" charset="utf-8"></script>

</head>
<body>

<script type="text/javascript">
    var btnList = ['layui-btn-warm','layui-btn-danger',"layui-bg-black","layui-bg-blue"];
    function dict(obj,types) {
        var str = "";
        var index = 0;
        if(types === "state" ){
            if(obj.state == 0){//下架
                index = 0;
                str = "下架";
            }
            if(obj.state == 1){//未通过
                index = 3;
                str = "未通过";
            }
            if(obj.state == 2){//通过
                index = 1;
                str = "通过";
            }
            if(obj.state == 3){//未审核
                index = 2;
                str = "未审核";
            }
            if(types=="img"){
                btn="<img style='width: 150px;height: 20px' src='"+obj.img+"'>";
            }
        }
        var btn = "<a class='layui-btn layui-btn-xs layui-btn-radius "+btnList[index]+"'>"+str+"</a>";
        console.log("btn");
        console.log(btn);
        return btn;
    }

</script>

<div class="layui-form searchTable">
            <span>
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="question" id="question" placeholder="问题" autocomplete="off" class="layui-input datePicker">
                    </div>
                </div>
                <button id="searchBtn" class="layui-btn" data-type="reload">查询</button>
            </span>

    <span class="layui-input-inline">
                <select name="state" id="state" lay-verify="required" style="width:100px; height: 38px;" class="layui-form-selectup">
                    <option value="-1" selected>全部</option>
                    <option  value="0">下架</option>
                    <option  value="1">未通过</option>
                    <option  value="2">通过</option>
                    <option  value="3">未审核</option>
                </select>
            </span>

    <button id="stateBtn" class="layui-btn" data-type="reload">搜索</button>

    <span>
        <button id="editor" class="layui-btn" data-type="reload" lay-event="update">审核</button>
    </span>

</div>


<table class="layui-hide" id="questionTable" lay-filter="question"></table>

<script>
    layui.use(['jquery','table','form','laydate'],function () {
        var table = layui.table;//表单
        var layer = layui.layer;
        var $ = layui.$;
        var laydate = layui.laydate;

        table.render({
            elem:'#questionTable',
            url:'/answer-audit-queryAll',
            method: 'post',
            toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
            defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            title: 'SysUser',
            cols: [
                [
                    {type: 'checkbox', fixed: 'left',width:50,align:'center'},
                    {field: 'id', title:'序号', align:'center',width:100},
                    {field: 'answerno', title:'编号', align:'center', width:100},
                    {field: 'issue', title:'问题', align:'center', width:100},
                    {field: 'content', title:'回答内容', align:'center', width:200},
                    {field: 'uid', title:'用户id', align:'center', width:100},
                    {field: 'state', title:'状态', align:'center', templet:function (data) {return dict(data,"state");}, width:100},
                    {field: 'createdate', title:'创建时间',width:250, align:'center'},
                    {field: 'account', title:'审核描述', align:'center', width:100},
                    {field: 'auditdate', title:'审核时间',width:250, align:'center'},

                    /*{fixed: 'right', title:'操作', toolbar: '#operation', width:150, align:'center'}*/
                ]
            ],
            page: true,//每页条数默认10
            request: {
                pageName :"currentPage",
                limitName : "pageSize"
            },
            parseData: function (res) {//res 即为原始返回的数据
                console.log(res);
                return {
                    "code": 0, //解析接口状态
                    "msg": "", //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": res.list //解析数据列表
                };
            }

        });

        //模糊查询
        var $  = layui.$;
        var active = {
            reload : function () {
                var vague = $("#question").val();
                var obj = {};
                if(vague != ""){
                    obj['question'] = vague;
                }
                if(vague == "" || vague == null){
                    layer.msg("请输入值");
                    return;
                }
                table.reload('questionTable',{
                    page: {
                        curr : 1
                    },
                    url: 'questionAudit-likeQuery/'+vague,
                    method : 'POST',
                    where : obj,
                    done : function (res,curr,count) {
                        this.where  = {};
                    }
                },'data');
            }
        };
        $("#searchBtn").on("click",function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : "";
        });

        //状态查询
        $("#stateBtn").on("click",function () {
            var state = $("#state").val();
            if(state == '' || state == null){
                layer.msg("请选择状态");
                return ;
            }
            var obj = {
                state: state
            };
            table.reload('questionTable',{
                page: {
                    curr : 1
                },
                url: 'questionAudit-stateQuery/'+state,
                method : 'POST',
                where : obj,
                done : function (res,curr,count) {
                    this.where  = {};
                }
            },'data');
        });


        /*
                //新增
                table.on("toolbar(SysUser)",function (object) {
                    switch(object.event){
                        case "add" ://表格头部事件为新增
                        {
                            var index = layer.open({
                                type:2,
                                area :['700px','450px'],
                                fixed: false,
                                maxmin: true,
                                content : '/sys/add'
                            });
                            layer.full(index);
                        }

                            break;

                        case 'LAYTABLE_TIPS' :
                            layer.alert('这是一个自定义的图标');
                            break;
                    }
                });*/


        $("#editor").on("click",function (object) {

            var checkStatus = table.checkStatus('questionTable');

            data = checkStatus.data

            var id;
            for(var i=0; i<data.length; i++){
                id =data[0].id
            }
            if(data.length > 1){
                layer.msg("只能选择一个")
                return;
            }

            if(data.length <=0){
                layer.msg("请选择一个")
                return;
            }
            var index;
            for (var i=0;i<data.length;i++){
                index=data[0].id
            }
            //iframe层-父子操作
            var editIndex = layer.open({
                type:2,//如果要打开一个html页面的话，这个type值就是2.如果要打开一个文本，这个值就是1.
                area :['700px','450px'],
                fixed: false,
                maxmin: true,
                content : 'answer-audit-audit/'+index
            });

            layer.full(editIndex);
        })



    })
</script>




</body>
</html>